package com.yanqu.road.server.manger.activity.mall;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.mall.ActivityMallTouchConfig;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eShopType;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.utils.RandomHelper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 *  描述：活动现金礼包
 *  作者：wmm
 *  时间：2024/11/29
 */
public class ActivityRmbMallTouchMgr extends TempMgr {

    //现金礼包 k:activityId k:type k:vip,v:MallInfo
    private static Map<Integer, Map<Integer, Map<Integer, MallInfo>>> rmbMallInfoTypeMap = new ConcurrentHashMap<>();

    private static Map<Integer, ActivityMallTouchConfig> touchConfigMap = new ConcurrentHashMap<>();

    //需要解析的活动类型及礼包类型
    private static Map<Integer, List<Integer>> activityMallTypeMap = new ConcurrentHashMap<>();

    @Override
    public boolean init() throws Exception {
//        activityMallTypeMap.put(eActivityType.XingYouShanShui.getValue(), Collections.singletonList(eShopType.XingYouShanShuiRmbMall.getValue()));
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static void reloadActivityData(){
        List<Integer> activityTypeList = new ArrayList<>(activityMallTypeMap.keySet());
        for (int activityType : activityTypeList) {
            List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(activityType);
            if(!openActivityInfoList.isEmpty()){
                reloadActivityMallConfig(openActivityInfoList.get(0));
            }
        }
    }

    public static void reloadActivityMallConfig(ActivityInfo activityInfo){
        //现金商城礼包加载
        int activityId = activityInfo.getActivityId();
        int activityType = activityInfo.getType();
        List<Integer> mallShopTypeList = activityMallTypeMap.get(activityType);
        if(mallShopTypeList == null || mallShopTypeList.isEmpty()){
            return;
        }
        if(!rmbMallInfoTypeMap.containsKey(activityId)){
            rmbMallInfoTypeMap.put(activityId, new ConcurrentHashMap<>());
        }
        Map<Integer, Map<Integer, MallInfo>> map = rmbMallInfoTypeMap.get(activityId);
        for (int mallShopType : mallShopTypeList) {
            Map<Integer, MallInfo> mallInfoMap = NormalActivityMgr.getMallInfoTypeMap(activityId, mallShopType);
            for(MallInfo item : mallInfoMap.values()) {
                String[] str = item.getExtendParam().split("\\|");
                int type = Integer.valueOf(str[0]);
                int vip = Integer.valueOf(str[1]);
                Map<Integer, MallInfo> typeMap = map.computeIfAbsent(type, k -> new ConcurrentHashMap<>());
                typeMap.put(vip,item);
            }
        }
        //获取活动config
        Map<String, ActivityConfig> configMap = NormalActivityMgr.getActivityConfigMap(activityInfo.getActivityId());
        if(configMap != null){
            ActivityMallTouchConfig config = new ActivityMallTouchConfig();
            if(configMap.containsKey("COMMON_MALL_KEEP_TIME")){
                config.setCOMMON_MALL_KEEP_TIME(configMap.get("COMMON_MALL_KEEP_TIME").getIntValue());
            }
            touchConfigMap.put(activityId, config);
        }
    }

    /**
     * 获取触发礼包ID
     * @param activityId
     * @param vipLevel
     * @return
     */
    public static int randomTriggerMallId(int activityId, int vipLevel) {
        Map<Integer, Map<Integer, MallInfo>> map = rmbMallInfoTypeMap.get(activityId);
        if(map == null){
            return 0;
        }
        Integer type = RandomHelper.getRandomElement(map.keySet());
        if (type == null) {
            return 0;
        }
        MallInfo targetMallInfo = null;
        int targetVipLevel = -1;
        Map<Integer, MallInfo> vipMap = map.get(type);
        for (Map.Entry<Integer, MallInfo> entry : vipMap.entrySet()) {
            MallInfo mallInfo = entry.getValue();
            int mallVipLevel = entry.getKey();
            if (vipLevel >= mallVipLevel && mallVipLevel > targetVipLevel) {
                targetMallInfo = mallInfo;
                targetVipLevel = mallVipLevel;
            }
        }
        if (targetMallInfo == null) {
            return 0;
        }
        return targetMallInfo.getMallId();
    }

    public static Map<Integer, Map<Integer, Map<Integer, MallInfo>>> getRmbMallInfoTypeMap() {
        return rmbMallInfoTypeMap;
    }

    public static Map<Integer, List<Integer>> getActivityMallTypeMap() {
        return activityMallTypeMap;
    }

    public static Map<Integer, ActivityMallTouchConfig> getTouchConfigMap() {
        return touchConfigMap;
    }

    public static ActivityMallTouchConfig getConfig(int activityId){
        return touchConfigMap.get(activityId);
    }
}
